//
//  ST_EditInfoTextFieldController.swift
//  btvideo
//
//  Created by mac on 2020/3/3.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit


//修改资料- UItextField 样式
class ST_EditInfoTextFieldController: ST_BTLightBarViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title_nav_btn.setTitleForAllStates(self.title ?? "")
        
        view.addSubview(text_field_root_view)
        text_field_root_view.snp.makeConstraints { (make) in
            make.height.equalTo(60)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(own_navigation_bar.snp.bottom).offset(11)
        }
        
        text_field_root_view.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 0, left: 15, bottom: 0, right: 15))
        }
        
        
        view.addSubview(counter_label)
        counter_label.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-20)
            make.top.equalTo(textField.snp.bottom).offset(10)
        }
        
        set_up_right_btn()
    }

 //    MARK:- public Properties
    
    var display_text = ""{
        didSet{
            textField.text = display_text
        }
    }
    
    
     var maxLength = 24
     
     var update_value_closure:((String)->())?
    
    var placeholder = ""{
        didSet{
            
            let attrString = NSMutableAttributedString(string: placeholder)
            
            let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 0.67, green: 0.71, blue: 0.75, alpha: 1)]
            
            attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
            
            textField.attributedPlaceholder = attrString
        }
    }
    
//    MARK: - Properties
    fileprivate lazy var field_placeholder_color = Color.init(hex: 0xACB4C0)!
    
    fileprivate lazy var field_placeholder_border_color = Color.init(hex: 0xF2F4F7)!
    
    fileprivate lazy var field_normal_border_color = Color.init(hex: 0xB2B9C4)!
    
    fileprivate lazy var text_field_root_view:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.clear
        control.cornerRadius = 10
        control.borderColor = field_placeholder_border_color
        control.borderWidth = 1
        return control
    }()
    
    fileprivate lazy var textField:UITextField = {
       let control = UITextField()
        control.textColor = UIColor.init(hex: 0x333333)
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        control.tintColor = UIColor.init(hex: 0x333333)
        control.delegate = self
        control.text = display_text
        return control
    }()
    
    fileprivate lazy var counter_label:UILabel = {
        let control = UILabel()
        
        var count = maxLength - display_text.count
        if count < 0{
            count = 0
        }
        control.text = "还有\(count)字"
        control.textColor = UIColor.init(hexString: "#ACB4C0")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    override func right_click() {
        
        guard let text = self.textField.text,!text.isEmpty else {
            return
        }
        
        if text.count > maxLength{
            ToastHud(text: "最多只能\(maxLength)个字", view: self.view)
            return
        }
        
        display_text = text
        
        self.update_value_closure?(display_text)
        
        goback_previous_controller()
    }
}

//MARK : -  Private Method
extension ST_EditInfoTextFieldController{
    
    
    fileprivate func set_up_right_btn(){
        
        right_nav_btn.setTitleForAllStates("完成")
        right_nav_btn.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        right_nav_btn.backgroundColor = Color.init(hexString: "#FEC207")
        right_nav_btn.cornerRadius = 15
        right_nav_btn.snp.remakeConstraints { (make) in
            make.right.equalTo(own_navigation_bar).offset(-15)
            make.centerY.equalTo(title_nav_btn)
            make.width.equalTo(60)
            make.height.equalTo(30)
        }
        right_nav_btn.isHidden = false
        
    }
    
    
    
}

//MARK: - Delegate
extension ST_EditInfoTextFieldController:UITextFieldDelegate{
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        
        text_field_root_view.borderColor = field_normal_border_color
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        
        text_field_root_view.borderColor = field_placeholder_border_color
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        let currentString: NSString = (textField.text ??  "" ) as NSString
        
        let newString: NSString =
            currentString.replacingCharacters(in: range, with: string) as NSString
        
        var residueCount = self.maxLength - newString.length
        
        if residueCount < 0{
            residueCount = 0
        }
        
        counter_label.text = "还有\(residueCount)字"
        
        return newString.length <= maxLength + 5
        
    }
    
}
